package main

import (
	"fmt"
	"time"

	"gitee.com/cjianwen/go/socks"
)

type msgHandler struct{}

func (h *msgHandler) OnConnect(sock socks.SocketConnection) {
	fmt.Println("connect ", sock.RemoteAddr().String())
}

func (h *msgHandler) OnDisconnect(sock socks.SocketConnection) {
	fmt.Println("disconnect ", sock.RemoteAddr().String())
}

func (h *msgHandler) OnMessage(sock socks.SocketConnection, msg *socks.PackData) {
	fmt.Println("on message ", string(msg.Body))
}

func main() {

	client := socks.NewSocketClient("127.0.0.1:8008", socks.SockOptions{HeadHandler: socks.NewDefaultHeadHandler(nil), MsgHandler: &msgHandler{}})

	client.Open()
	b := false
	cnt := 0
	for {
		if client.Connected() {
			if b {
				if client.GetConnection().SendRawData([]byte("this is a test")) {
					fmt.Println("send raw msg ok")
					cnt++
					if cnt >= 3 {
						break
					}
				} else {
					fmt.Println("send raw msg failed")
				}
			} else {
				if client.GetConnection().SendData([]byte("this is a test")) {
					fmt.Println("send msg ok")
					// break
				} else {
					fmt.Println("send msg failed")
				}
			}
			b = !b
		} else {
			fmt.Println("wait connnect")
		}
		time.Sleep(time.Second)
	}

	time.Sleep(time.Second * 30)
	fmt.Println("client exit")
}
